package com.ruoyi.web.controller.system;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.base.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.page.TableDataInfo;
import com.ruoyi.framework.util.FileUploadUtils;
import com.ruoyi.framework.web.base.BaseController;
import com.ruoyi.system.domain.SysBanner;
import com.ruoyi.system.service.ISysBannerService;

@Controller
@RequestMapping("/system/banner")
public class SysBannerController extends BaseController{
	
	private static final Logger log = LoggerFactory.getLogger(SysBannerController.class);

	
	@Autowired
	private ISysBannerService bannerService;
	
	private String prefix = "system/banner";
	
	
	@GetMapping()
	@RequiresPermissions("system:banner:view")
	public String bannnerList() {
		return prefix+"/banner";
	}
	
	@PostMapping("/list")
	@RequiresPermissions("system:banner:list")
	@ResponseBody
	public TableDataInfo list(SysBanner banner) {
		startPage();
        List<SysBanner> list = bannerService.selectBannerAll(banner);
        return getDataTable(list);
	}
	
	@GetMapping("/add")
	public String add() {
		return prefix +"/add";
	}
	
	@PostMapping("/save")
	@RequiresPermissions("system:banner:add")
	@Log(title = "文件上传", businessType = BusinessType.INSERT)
	@ResponseBody
	public AjaxResult addSave(MultipartFile file,SysBanner banner) {
		boolean flag = false;
		String suffer = FileUploadUtils.dealName(file.getOriginalFilename());
		try {
			String dest = FileUploadUtils.upload(FileUploadUtils.getDefaultBaseDir(), file, suffer);
			if(dest==null) {
				throw new Exception("文件上传失败");
			}else {
				banner.setBannerUrl(dest);
				bannerService.insertBanner(banner);
				flag = true;
			}
			
			System.out.println("dest"+dest);
		} catch (Exception exception) {
			log.error("文件上传失败！");
			exception.printStackTrace();
		} 
		return toAjax(flag);
	}
	
	
	@Log(title = "文件上传",businessType = BusinessType.DELETE)
	@RequiresPermissions("system:banner:remove")
	@RequestMapping("/remove")
	@ResponseBody
	public AjaxResult removeAll(String ids) {
		bannerService.removeBanner(ids);
		return toAjax(true);
	}
	
	
	@GetMapping("/edit/{id}")
	public String  edit(@PathVariable("id") Integer id,Model model) {
		SysBanner banner = bannerService.selectOneById(id);
		if(banner.getBannerUrl().contains(":")) {
			String url = banner.getBannerUrl().split(":")[1];
			url = url.replaceAll("\\\\", "/");
			banner.setBannerUrl(url);
		}
		model.addAttribute("banner", banner);
		return prefix+"/edit";
	}
	
	@Log(title = "文件上传",businessType = BusinessType.UPDATE)
	@RequiresPermissions("system:banner:edit")
	@ResponseBody
	@Transactional(noRollbackFor = RuntimeException.class)
	@PostMapping("/edit")
	public AjaxResult edit(MultipartFile file,SysBanner banner) {
		boolean flag = false;
		try {
			SysBanner b = bannerService.selectOneById(banner.getId());
			String suffer = FileUploadUtils.dealName(file.getOriginalFilename());
			String dest = FileUploadUtils.upload(FileUploadUtils.getDefaultBaseDir(), file, suffer);
			if(dest==null) {
				throw new Exception("文件上传失败");
			}else {
				banner.setBannerUrl(dest);
				bannerService.updateBannerById(banner);
				if(banner.getId() != null) {
					bannerService.removeFileByurl(b.getBannerUrl());
				}
				flag = true;
			}
			
			System.out.println("dest"+dest);
		} catch (Exception exception) {
			log.error("文件上传失败！");
			exception.printStackTrace();
		} 
		return toAjax(flag);
	}
}
